#!/usr/bin/perl
#
# Make a statistics file for languages - status of translation
#
# Intended to be read by language chooser routines.
#
use Getopt::Long;
$DEBUG = 0;

GetOptions("debug" => \$DEBUG);

chdir($ENV{COUNTER_ROOT});
@langs = <i18n/translations/*/messages.po>;

#warn "Languages:\n", join("\n", @langs), "\n";

for $lang (@langs) {
    my $fuzz;
    next if $lang =~ /Main/;
    $statistic = `LANGUAGE= msgfmt --statistics $lang 2>&1`;
    $DEBUG && warn $lang . ":" . $statistic;
    chomp $statistic;
    if ($statistic =~ /(\d+) translated messages, (\d+) fuzzy translations, (\d+) untranslated messages/) {
	$score = $1 / ($1 + $2 + $3);
	$fuzz = $2 / ($1 + $2 + $3);
    } elsif ($statistic =~ /(\d+) translated messages.* (\d+) untranslated messages/) {
	$score = $1 / ($1 + $2);
    } elsif ($statistic =~ /\d+ translated messages.$/) {
	$score = 1;
    } else {
	warn "No parsing of stats for $lang: $statistic\n";
	next;
    }
    # derive the language from the directory name. Argh.
    if ($lang =~ m!/([^/]+)/messages.po!) {
	$langid = $1;
	$langid = "no_NO" if ($langid eq "norwegian");
	$langid = "nl_NL" if ($langid eq "dutch");
	$score{$langid} = $score;
	$fuzz{$langid} = $fuzz if defined($fuzz);
    } else {
	warn "No langid in $lang\n";
    }
}

for $lng (sort(keys(%score))) {
    if ($fuzz{$lng}) {
	printf("%s: %0.2f, %0.2f fuzz\n",  $lng, $score{$lng}*100,
	       $fuzz{$lng} * 100);
    } else {
	printf("%s: %0.2f\n",  $lng, $score{$lng}*100);
    }
}

